package com.tmc.platform.ctrip.controller;

import java.util.Date;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;
import com.tmc.platform.ctrip.CtripServiceImpl;
import com.tmc.platform.ctrip.bean.OpenIssueBillInfoResponse;
import com.tmc.platform.ctrip.bean.OpenIssueOrderListResponse;
import com.tmc.platform.ctrip.bean.OpenSearchIssueBillResponse;
import com.tmc.platform.ctrip.util.XStreamUtil;
import com.tmc.util.DateUtil;
import com.tmc.util.XMLUtil;

@Controller
@RequestMapping("/ctrip")
public class CtripController {

	@Autowired
	private CtripServiceImpl ctripServiceImpl;
	
	/**
	 * 今日出票单接口
	 * @param orderBeginDateTime
	 * @param orderEndDateTime
	 * @return
	 */
	@RequestMapping(value = "/openIssueOrderList", produces = "text/html;charset=UTF-8")
	@ResponseBody
	public String openIssueOrderList(@RequestParam(required=true,value="orderBeginDateTime")String orderBeginDateTime,
									 @RequestParam(required=true,value="orderEndDateTime")String orderEndDateTime){
		String resp = "";
		try {
			Date beginDate = DateUtil.format(orderBeginDateTime,"yyyy-MM-dd HH:mm:ss");
			Date endDate = DateUtil.format(orderEndDateTime, "yyyy-MM-dd HH:mm:ss");
			resp = new String(ctripServiceImpl.openIssueOrderList(beginDate,endDate).getBytes(),"utf-8");
			String xml = XMLUtil.getPartXmlString("OpenIssueOrderListResponse",resp);
			OpenIssueOrderListResponse openIssueOrderListResponse = XStreamUtil.fromXml(xml);
			resp = JSON.toJSONString(openIssueOrderListResponse);
			System.out.println(resp);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return resp;
	}
	
	/**
	 * 出票单
	 * 详情接口
	 * @param issueBillID
	 * @return
	 */
	@RequestMapping(value = "/issueBillInfo", produces = "text/html;charset=UTF-8")
	@ResponseBody
	public String issueBillInfo(@RequestParam(required=true,value="issueBillID")Long issueBillID){
		String resp = "";
		try {
			String result = ctripServiceImpl.issueBillInfo(issueBillID);
			resp = new String(result.getBytes(),"utf-8");
			String xml = XMLUtil.getPartXmlString("OpenIssueBillInfoResponse",resp);
			OpenIssueBillInfoResponse openIssueBillInfoResponse = XStreamUtil.fromXml(xml);
			resp = JSON.toJSONString(openIssueBillInfoResponse);
			System.out.println(resp);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return resp;
	}
	
	/**
	 * 退票查询接口
	 */
	@RequestMapping(value= "/refundWaitConfirmList", produces = "text/html;charset=UTF-8")
	@ResponseBody
	public String refundWaitConfirmList(@RequestParam(required=true,value="isInternational")Integer isInternational,
										@RequestParam(required=true,value="startRefundDate")String startRefundDate,
										@RequestParam(required=true,value="endRefundDate")String endRefundDate){
		String resp = "";
		try {
			Date StartDate = DateUtil.format(startRefundDate,"yyyy-MM-dd");
			Date endDate = DateUtil.format(endRefundDate, "yyyy-MM-dd");
			resp = new String(ctripServiceImpl.refundWaitConfirmList(isInternational, StartDate, endDate).getBytes(),"utf-8");
		} catch (Exception e) {
			e.printStackTrace();
		}
		return resp;
	}
	
	/**
	 * 退票确认接口
	 * @param orderId
	 * @param prid
	 * @return
	 */
	@RequestMapping(value="/refundConfirm", produces = "text/html;charset=UTF-8")
	@ResponseBody 
	public String refundConfirm(@RequestParam(required=true,value="orderId")String orderId, 
								@RequestParam(required=true,value="prid")String prid){
		String resp = "";
		try{
			resp = ctripServiceImpl.refundConfirm(orderId,prid);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return resp;
	}
	
	/**
	 * 票号回填接口
	 * @param bookingChannel
	 * @param extOrderID
	 * @param issueBillID
	 * @param dataChangeType
	 * @param pnr
	 * @param pnrResultStatus
	 * @param officeNo
	 * @param sequence
	 * @param dPort
	 * @param aPort
	 * @param flightNo
	 * @param subClass
	 * @param checkChannel
	 * @param ticketNo
	 * @param airLineCode
	 * @param ticketResultStatus
	 * @param passengerName
	 * @param agencyTicketTax
	 * @param lcType
	 * @return
	 */
	@RequestMapping(value="/searchModifyOrderInfo", produces = "text/html;charset=UTF-8")
	@ResponseBody
	public String searchModifyOrderInfo(@RequestParam(required=false,value="bookingChannel")String bookingChannel,
										@RequestParam(required=false,value="extOrderID")String extOrderID,
										@RequestParam(required=true,value="issueBillID")Long issueBillID,
										@RequestParam(required=true,value="dataChangeType")Integer dataChangeType,
										@RequestParam(required=false,value="pnr")String pnr,
										@RequestParam(required=false,value="pnrResultStatus")Integer pnrResultStatus,
										@RequestParam(required=true,value="officeNo")String officeNo,
										@RequestParam(required=true,value="sequence")Integer sequence,
										@RequestParam(required=true,value="dPort")String dPort,
										@RequestParam(required=true,value="aPort")String aPort,
										@RequestParam(required=true,value="flightNo")String flightNo,
										@RequestParam(required=true,value="subClass")String subClass,
										@RequestParam(required=false,value="checkChannel")String checkChannel,
										@RequestParam(required=true,value="ticketNo")String ticketNo,
										@RequestParam(required=true,value="airLineCode")String airLineCode,
										@RequestParam(required=false,value="ticketResultStatus")Integer ticketResultStatus,
										@RequestParam(required=true,value="passengerName")String passengerName,
										@RequestParam(required=true,value="agencyTicketTax")Integer agencyTicketTax,
										@RequestParam(required=true,value="lcType")String lcType){
		String resp = "";
		try{
			resp = ctripServiceImpl.SearchModifyOrderInfo(bookingChannel, extOrderID, issueBillID, dataChangeType, pnr, pnrResultStatus, officeNo, sequence, dPort, aPort, flightNo, subClass, checkChannel, ticketNo, airLineCode, ticketResultStatus, passengerName, agencyTicketTax, lcType);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return resp;
	}
	
	/**
	 * 改签处理查询列表接口
	 * @param fromDateTime
	 * @param toDateTime
	 * @param rebookQueryOptionType
	 * @param orderID
	 * @param ticketNO
	 * @return
	 */
	@RequestMapping(value="/searchExchangeList", produces = "text/html;charset=UTF-8")
	@ResponseBody
	public String searchExchangeList(@RequestParam(required=true,value="fromDateTime")String fromDateTime,
									 @RequestParam(required=true,value="toDateTime")String toDateTime,
									 @RequestParam(required=true,value="rebookQueryOptionType")String rebookQueryOptionType,
									 @RequestParam(required=false,value="orderID")Integer orderID,
									 @RequestParam(required=false,value="ticketNO")String ticketNO){
		String resp = "";
		try{
			Date fromDate = DateUtil.format(fromDateTime,"yyyy-MM-dd HH:mm:ss");
			Date toDate = DateUtil.format(toDateTime,"yyyy-MM-dd");
			resp = new String(ctripServiceImpl.searchExchangeList(fromDate, toDate, rebookQueryOptionType, orderID, ticketNO).getBytes(),"utf-8");
		} catch (Exception e) {
			e.printStackTrace();
		}
		return resp;
	}
	
	/**
	 * 改签操作接口
	 * @param rebookProcessStatusType
	 * @param rbkId
	 * @param remark
	 * @param sequence
	 * @param airLineCode
	 * @param airLine
	 * @param passengerName
	 * @param flight
	 * @param dPort
	 * @param aPort
	 * @param takeOffTime
	 * @param subClass
	 * @param ticketNO
	 * @param recordNo
	 * @param rebookingQueryFee
	 * @param rebookType
	 * @return
	 */
	@RequestMapping(value="/modifyExchangeList", produces = "text/html;charset=UTF-8")
	@ResponseBody
	public String modifyExchangeList(@RequestParam(required=true,value="rebookProcessStatusType")String rebookProcessStatusType,
									 @RequestParam(required=true,value="rbkId")String rbkId, 
									 @RequestParam(required=true,value="remark")String remark, 
									 @RequestParam(required=true,value="sequence")Integer sequence,
									 @RequestParam(required=true,value="airLineCode")String airLineCode,
									 @RequestParam(required=true,value="airLine")String airLine,
									 @RequestParam(required=true,value="passengerName")String passengerName,
									 @RequestParam(required=true,value="flight")String flight,
									 @RequestParam(required=true,value="dPort")String dPort,
									 @RequestParam(required=true,value="aPort")String aPort,
									 @RequestParam(required=true,value="takeOffTime")String takeOffTime,
									 @RequestParam(required=true,value="subClass")String subClass,
									 @RequestParam(required=true,value="ticketNO")String ticketNO,
									 @RequestParam(required=true,value="recordNo")String recordNo,
									 @RequestParam(required=true,value="rebookingQueryFee")Float rebookingQueryFee,
									 @RequestParam(required=false,value="rebookType") String rebookType){
		String resp = "";
		try{
			resp = new String(ctripServiceImpl.modifyExchangeList(rebookProcessStatusType, rbkId, remark, sequence, airLineCode, airLine, passengerName, flight, dPort, aPort,DateUtil.format(takeOffTime,"yyyy-MM-dd HH:mm:ss"), subClass, ticketNO, recordNo, rebookingQueryFee, rebookType).getBytes(),"utf-8");
		} catch (Exception e) {
			e.printStackTrace();
		}
		return resp;
	}
	
	/**
	 * 改签确认接口
	 * @param rbkid
	 * @param modifyRemark
	 * @param passengername
	 * @param agencyTotalRebookFee
	 * @return
	 */
	@RequestMapping(value="/confirmRebookAmountService", produces = "text/html;charset=UTF-8")
	@ResponseBody
	public String confirmRebookAmountService(@RequestParam(required=true,value="rbkid")Integer rbkid,
											 @RequestParam(required=true,value="modifyRemark")String modifyRemark,
											 @RequestParam(required=true,value="passengername")String passengername,
											 @RequestParam(required=true,value="agencyTotalRebookFee")Float agencyTotalRebookFee){
		String resp = "";
		try{
			resp = ctripServiceImpl.confirmRebookAmountService(rbkid, modifyRemark, passengername, agencyTotalRebookFee);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return resp;
	}	
	
	/**
	 * 出票单查询接口 
	 * @param orderBeginDateTime
	 * @param orderEndDateTime
	 * @param issueBillID
	 * @param issueStatus
	 * @return
	 */
	@RequestMapping(value="/searchIssueBill", produces = "text/html;charset=UTF-8")
	@ResponseBody
	public String searchIssueBill(@RequestParam(required=true,value="orderBeginDateTime") String orderBeginDateTime,
			                      @RequestParam(required=true,value="orderEndDateTime") String orderEndDateTime, 
			                      @RequestParam(required=true,value="issueBillID")Long issueBillID, 
			                      @RequestParam(required=false,value="issueStatus")String issueStatus){
		String resp = "";
		try{
			Date beginDate = DateUtil.format(orderBeginDateTime,"yyyy-MM-dd HH:mm:ss");
			Date endDate = DateUtil.format(orderEndDateTime,"yyyy-MM-dd HH:mm:ss");
			resp =new String(ctripServiceImpl.searchIssueBill(beginDate, endDate, issueBillID, issueStatus).getBytes(),"utf-8"); 
			String xml = resp.substring(resp.indexOf("<OpenSearchIssueBillResponse>"), resp.indexOf("</OpenSearchIssueBillResponse>") + 30);
			OpenSearchIssueBillResponse OpenSearchIssueBillResponse = XStreamUtil.fromXml(xml);
			resp = JSON.toJSONString(OpenSearchIssueBillResponse);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return resp;
	}
	
} 
